package com.example.presentshopping.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.base.MyApplication
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.tool.LogUtils
import kotlinx.android.synthetic.main.activity_location.*
import kotlinx.android.synthetic.main.activity_location.title_main_tv
import kotlinx.android.synthetic.main.activity_setup_pay_pwd_check.*
import kotlinx.android.synthetic.main.activity_setup_pay_pwd_check.et_SMSCode
import kotlinx.android.synthetic.main.activity_setup_pay_pwd_check.et_phone
import kotlinx.android.synthetic.main.activity_setup_pay_pwd_check.tv_getSMSCode
import kotlinx.android.synthetic.main.activity_setup_pay_pwd_check.tv_login
import kotlinx.android.synthetic.main.login_activity.*

class SetupPayPwdCheckActivity : BaseMvpActivity() {
    private var countdown = 0
    private var sendMesPresenter: PublicPresenter? = null
    override val layoutId: Int
        get() = R.layout.activity_setup_pay_pwd_check

    override fun initView(savedInstanceState: Bundle?) {
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        sendMesPresenter = PublicPresenter(context, false, "")
        sendMesPresenter?.attachView(this)
        et_phone.text =MyApplication.userInfoBean?.data?.mobile
        tv_login.setOnClickListener {

                if (!TextUtils.isEmpty(et_SMSCode.text)){
                 if (!TextUtils.isEmpty(et_passd.text)){
                     if (et_passd.text.length == 6){
                         sendMesPresenter?.netWorkRequestPost(HttpURL.Req_editpwd,HttpRequest.gteditpwd(et_passd.text.toString(),et_SMSCode.text.toString()))

                     }else{
                         showToast("请输入六位数支付密码")
                     }
                 }else{
                     showToast("请输入六位数支付密码")
                 }

                }else{
                    showToast("请输入验证码")
                }
            }
        tv_getSMSCode.setOnClickListener { sendMes() }
    }



    override fun onDestroy() {
        sendMesPresenter?.detachView()
        super.onDestroy()
    }


    override fun showToast(msg: String?) {
     ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        showToast("密码修改成功")
        finish()
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }


    // 验证码倒计时。
    private fun startTimer() {
        countdown = 60
        //tv_Sent_SMS.setTextColor(getResources().getColor(R.color.colorMain));
        tv_getSMSCode.isClickable = false
        object : CountDownTimer(1000 * 60, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                try {
                    if (countdown != -1) {
                        countdown--
                        tv_getSMSCode.text = countdown.toString() + "s"
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            override fun onFinish() {
                try {
                    //tv_Sent_SMS.setTextColor(getResources().getColor(R.color.colorMain));
                    tv_getSMSCode.text = resources.getString(R.string.ToResend)
                    tv_getSMSCode.isClickable = true
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }.start()
    }

    /**
     * 发送短信
     */
    private fun sendMes() {
        // 获取短信
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getPwdCaptcha,
            HttpRequest.SendSms(""), object : TaskCallback {
                override fun onSuccess(data: String?) {
                    // 验证码已发送
                    showToast("验证码已发送至账号绑定手机号码")
                    // 验证码倒计时。
                    startTimer()
                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {
                    showToast(msg)
                }
            })
    }


}